package com.crl.rcdp.sys.controller;

import com.cars.util.json.JsonResult;
import com.cars.util.json.JsonResultUtil;
import com.cars.util.jwt.JwtUtil;
import com.cars.util.string.StringUtil;
import com.crl.rcdp.sys.dto.SysUserDTO;
import com.crl.rcdp.sys.dto.SysUserLoginDTO;
import com.crl.rcdp.sys.service.SysUserLoginService;
import com.crl.rcdp.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @description:
 * @author: 孔垂云
 * @version: 1.0
 * @date: 2020/6/21 16:48
 */
@RestController
//@RequestMapping("/sys/login")
public class SysLoginController {
    @Autowired
    private SysUserLoginService sysUserLoginService;
    @Autowired
    private SysUserService sysUserService;

    /**
     * 登录校验
     */
    @PostMapping("/check")
    public JsonResult check(@RequestBody SysUserLoginDTO sysUserLoginDTO) {
        return sysUserLoginService.checkLogin(sysUserLoginDTO);
    }

    /**
     * 获取用户信息
     *
     * @param map
     * @return
     */
    @PostMapping("/info")
    public JsonResult info(HttpServletRequest request, @RequestBody Map<String, String> map) {
        String accessToken = request.getHeader("Authorization");//获取token
        Integer userId = JwtUtil.getUserId(accessToken);
        SysUserDTO sysUserDTO = sysUserService.getUserLogin(userId);
        return JsonResultUtil.ok(sysUserDTO);
//        return "{\"returnCode\":\"00200\",\"data\":{\"roles\":[\"admin\"],\"introduction\":\"I am a super administrator\",\"avatar\":\"https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif\",\"name\":\"Super Admin\"}}";
    }

    /**
     * 注销
     * @param request
     * @return
     */
    @RequestMapping("/logout")
    public JsonResult logout(HttpServletRequest request) {
        String accessToken = request.getHeader("Authorization");//获取token
        if (StringUtil.isNotNullOrEmpty(accessToken)) {
            sysUserLoginService.logout(accessToken);
        }
        return JsonResultUtil.ok();
    }

}
